package org.dromara.third.convert.earthWork.web;

import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.exception.BusinessServiceException;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.utils.ValidateUtil;
import org.dromara.system.domain.EarthWork;
import org.dromara.system.mapper.EarthWorkMapper;
import org.dromara.third.auth.beans.constans.ExceptionEnum;
import org.dromara.third.auth.common.auth.TokenThreadLocal;
import org.dromara.third.auth.util.AESUtil;
import org.dromara.third.convert.earthWork.dto.EarthWorkDTO;
import org.dromara.third.convert.earthWork.validator.EarthWorkValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RequiredArgsConstructor
@RestController
@Slf4j
@RequestMapping("/api/v1")
public class ConvertEarthWorkController {

    @Autowired
    private EarthWorkMapper earthWorkMapper;

    @PostMapping("/earthWork")
    public R<Void> addEarthWork(@RequestBody String jsonReq) {
        String orgCode = TokenThreadLocal.getThreadLocal();
        EarthWorkDTO earthWorkDTO;
        try {
            String decrypt = AESUtil.decrypt(jsonReq);
            earthWorkDTO = JSONObject.parseObject(decrypt, EarthWorkDTO.class);
        } catch (Exception e) {
            log.info("解析参数异常");
            throw new BusinessServiceException(ExceptionEnum.AuthException06.getCode(), ExceptionEnum.AuthException06.getMessage());
        }
        // 校验参数
        List<String> errorMessageList = new ArrayList<>();
        ValidateUtil.validate(errorMessageList, earthWorkDTO, new EarthWorkValidator());
        if (!CollectionUtils.isEmpty(errorMessageList)) {
            String errorMessage = StringUtils.join(errorMessageList, ",");
            throw new BusinessServiceException(ExceptionEnum.AuthException06.getCode(), errorMessage);
        }
        EarthWork earthWork = assembleEarthWorkData(orgCode, earthWorkDTO);
        int count = earthWorkMapper.countWithOutDelFlag(earthWork.getId());
        if (count == 0) {
            earthWorkMapper.insert(earthWork);
        } else {
            earthWorkMapper.updateWithOutDelFlag(earthWork);
        }
        return R.ok();
    }

    private EarthWork assembleEarthWorkData(String orgCode, EarthWorkDTO dto) {
        EarthWork earthWork = new EarthWork();
        try {
            earthWork.setId(dto.getZyid());
            earthWork.setZyzbh(dto.getZyzbh());
            earthWork.setSqdw(dto.getSqdw());
            earthWork.setZysqsj(DateUtils.parseDate(dto.getZysqsj()));
            earthWork.setSqr(dto.getSqr());
            earthWork.setZynr(dto.getZynr());
            earthWork.setZywz(dto.getZywz());
            earthWork.setZydwlx(dto.getZydwlx());
            earthWork.setZydwmc(dto.getZydwmc());
            earthWork.setZyfzr(dto.getZyfzr());
            earthWork.setZysskssj(DateUtils.parseDate(dto.getZysskssj()));
            earthWork.setZyssjssj(DateUtils.parseDate(dto.getZyssjssj()));
            earthWork.setZyfwFs(dto.getZyfwFs());
            earthWork.setZyjt(dto.getZyjt());
            earthWork.setSfsjqttszy(dto.getSfsjqttszy());
            earthWork.setSjqttszyzbh(dto.getSjqttszyzbh());
            earthWork.setFxbs(dto.getFxbs());
            earthWork.setAqjdr(dto.getAqjdr());
            earthWork.setJsjdr(dto.getJsjdr());
            earthWork.setJhr(dto.getJhr());
            earthWork.setDqzt(dto.getDqzt());
            earthWork.setLongitude(dto.getLongitude());
            earthWork.setLatitude(dto.getLatitude());
            earthWork.setStartTime(DateUtils.parseDate(dto.getStartTime()));
            earthWork.setEndTime(DateUtils.parseDate(dto.getEndTime()));
            earthWork.setSpgbbm(dto.getSpgbbm());
            earthWork.setFhyp(dto.getFhyp());
            earthWork.setAttachment(dto.getAttachment());
            earthWork.setDelFlag(Long.parseLong(dto.getDelFlag()));
            earthWork.setCreateDept(Long.parseLong(dto.getSsbm()));
            earthWork.setCreateBy(Long.parseLong(dto.getCreateBy()));
            earthWork.setCreateTime(DateUtils.parseDate(dto.getCreateTime()));
            earthWork.setUpdateBy(Long.parseLong(dto.getUpdateBy()));
            earthWork.setUpdateTime(DateUtils.parseDate(dto.getUpdateTime()));
            earthWork.setTenantId(orgCode);
        } catch (Exception e) {
            throw new BusinessServiceException(ExceptionEnum.AuthException07.getCode(), "组装动土作业信息异常");
        }
        return earthWork;
    }
}
